<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    dialog-width="600px"
    :page-show="pageShow"
    @dialog-close="dialogClose"
  >
    <template #toolPanel />
    <template #searchPanel>
      <search-panel @search="search" @insert="handleDataInsert" :left="true">
            <el-form-item label="用户名称:">
              <el-input v-model="filters.name" style="width: 200px" clearable placeholder="用户名称" />
            </el-form-item>
            <el-form-item label="联系电话:">
              <el-input v-model="filters.phonenum" style="width: 200px" clearable placeholder="联系电话" />
            </el-form-item>
            <el-form-item label="用户类型:">
              <el-select v-model="filters.usertype" clearable>
                <el-option
                  v-for="item in usertypes"
                  :key="item.id"
                  :value="item.id"
                  :label="item.value" />
              </el-select>
            </el-form-item>
<!--            <el-form-item label="状态:">-->
<!--              <el-select v-model="filters.userstatus" clearable>-->
<!--                <el-option-->
<!--                  v-for="item in supplierList"-->
<!--                  :key="item.id"-->
<!--                  :value="item.id"-->
<!--                  :label="item.value"-->
<!--                />-->
<!--              </el-select>-->
<!--            </el-form-item>-->
            <el-form-item label="备注:">
              <el-input v-model="filters.remark" maxlength="100" clearable />
            </el-form-item>
<!--          <el-form-item>-->
<!--            <el-button type="primary" size="small" round @click="setTableData()">查询</el-button>-->
<!--          </el-form-item>-->

      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :current="start"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="150"
      >
        <template #action="{row}">
<!--          <el-button type="text" >启用</el-button>-->
          <el-button size="small" round @click="editRow(row)">编辑</el-button>
          <el-button size="small" round style="color:red;" @click="deletedRow(row)">删除</el-button>
        </template>
      </data-table>
    </template>

    <template #dialog>
      <el-form ref="form" :model="form" label-width="100px" style="display: inline-block"  :hide-required-asterisk="true" :show-message="false">
        <el-form-item required label="用户姓名" prop="username">
          <el-input v-model="form.username" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item required label="用户账号" prop="phonenum">
          <el-input v-model="form.phonenum" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码:" prop="password" style="display: inline-block">
          <el-input type="password" maxlength="20"  v-model="form.password" style="width: 450px;"></el-input>
        </el-form-item>
        <el-form-item required label="用户类型:"  prop="usertype" style="display: inline-block">
          <el-select clearable v-model="form.usertype" placeholder="用户类型" style="width: 450px;">
            <el-option
              v-for="item in usertypes"
              :key="item.id"
              :value="item.id"
              :label="item.value"
            />
          </el-select>
        </el-form-item>
        <!--    <el-form-item required label="用户状态:"  prop="userstatus" style="display: inline-block">-->
        <!--      <el-select clearable v-model="form.userstatus" placeholder="用户状态" style="width: 450px;">-->
        <!--        <el-option-->
        <!--          v-for="item in menus"-->
        <!--          :key="item.id"-->
        <!--          :value="item.id"-->
        <!--          :label="item.content"-->
        <!--        />-->
        <!--      </el-select>-->
        <!--    </el-form-item>-->
        <el-form-item label="用户备注:">
          <el-input type="textarea" maxlength="200" :autosize="{ minRows: 3, maxRows: 6}" v-model="form.remark"></el-input>
        </el-form-item>
      </el-form>
      <el-row style="text-align: center;">
        <el-button size="small" @click="dialogVisible = false" round>取 消</el-button>
        <el-button  size="small" round type="primary" @click="onSubmit">保存</el-button>
      </el-row>
    </template>

  </base-layout>
</template>

<script>
import baseMixin from '@/mixins/base'
import DialogDataForm from './components/DialogDataForm'
import { findUser,deleteUser,addSystemUser,editSystemUser} from '@/api/user'
var crypto = require('crypto');

export default {
  name: 'SystemManager',
  components: { DialogDataForm },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: findUser,
      deleteDataRow: deleteUser,
      refresh: '',
      format(res) {
        return res.map(data => {
          return data
        })
      }
    })
  ],
  data() {
    return {
      buttons: [],
      storeList: [],
      supplierList: [],
      jewelryCategoryList: [],
      pageShow: true,
      dialogData:{},
      //用户类型 1 管理员 2超级管理员 3 下游渠道商 4 上游渠道商 5宽带业务 6慢充业务
      //,{id:2,value:'超级管理员'},{id:3,value:'下游渠道商'},{id:4,value:'上游渠道商'}
      usertypes:[{id:1,value:'管理员'},{id:5,value:'客服'},{id:6,value:'财务'}],
      tableColumnAttributes: [
        { name: '用户名称', value: 'username'  },
        { name: '联系电话', value: 'phonenum' },
        { name: '用户类型', value: 'usertype', formatter: this.formatUserType },
        { name: '状态', value: 'userstatus' , formatter: this.formatUserStatus},
        { name: '创建时间', value: 'createtime' ,width:150},
        { name: '备注', value: 'remark' }
      ],
      filters: {
        usertype:'',
        name:'',
        phonenum:'',
        userstatus:'',
        remark:''
      },
      form: {
        usertype:'',
        name:'',
        phonenum:"",
        userstatus:'',
        remark:''
      }
    }
  },
  methods: {
    search(form){
      let para = {
        shopName: this.filters.name,
        start: 0,
        pageSize: 10,
        phonenum: this.filters.phonenum,
        userstatus:'',
        usertype: this.filters.usertype,
        remark : this.filters.remark
      };
      this.searchOptions = para;
      this.setTableData()
    },
    formatUserType (row, column) {
      //1 管理员 2超级管理员 3 下游渠道商
      return row.usertype == 1 ? '管理员' : row.usertype == 2 ? '超级管理员' : row.usertype == 3 ? '分销商':  row.usertype == 5 ? "宽带业务下游" : "供货商";
    },
    formatUserStatus (row, column) {
      return row.userstatus === 1 ? '正常' : row.userstatus === 2 ? '禁用' : "其他";
    },
    deletedRow(row){
      row.userstatus = 0;
      this.deletedRowtable(row);
    },
    onSubmit(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {roundButton:true}).then(() => {
            let para = Object.assign({}, this.form);
            if(para.password){
              var md5 = crypto.createHash('md5');
              para.password = md5.update(para.password).digest('hex');
            }else{
              para.password = "";
            }
            if(para.useruuid){
              editSystemUser(para).then((res) => {
                this.commitResult(res);
              });
            }else{
              addSystemUser(para).then((datas) => {
                this.commitResult(datas);
              }).catch(function (error) {
                console.log(error);
              });
            }
          });
        }
      });
    },
    commitResult(datas){
      this.resultApply(datas,true);
      this.$refs['form'].resetFields();
    },
    editRow(row){
      this.dialogTitle = "编辑";
      this.dialogVisible = true;
      this.form = row;
    }
  }
}
</script>
